<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Requests\LoveUserRequest;

class LovePersonController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        //每页显示
        $num = $request->input('num',8);
        //判断是否有关键词
        if($request->input('keywords')){

            $cates = DB::table('loveperson')->where('name','like','%'.$request->input('keywords').'%')->orderBy('time','desc')->paginate($num);

        }else{

            //查询数据
            $cates = DB::table('loveperson')->orderBy('time','desc')->paginate($num);
            
        }

        $data = $request->except('page');
        // dd($data);
        return view('Admin.loveperson.index',['cates'=>$cates,'data'=>$data]);
    }

    //志愿者添加
    public function getAdd()
    {
        return view('Admin.loveperson.add');
    }

    //信息添加 

    public function postInsert(Request $request)

    public function postInsert(LoveUserRequest $request)

    {
        // dd($request->all());
        $data = $request->except('_token');
        $time = time();
        $data['time'] = $time;

        //数据库操作
        $res = DB::table('loveperson')->insert($data);

        if($res){

            return redirect('/admin/loveperson/index')->with('success','添加成功');
        }else{

            return back()->with('error','添加失败');
        }
    }
    

    //删除操作
    public  function postDel(Request $request)
    {
        $res = DB::table('loveperson')->where('id','=',$request->id)->delete();
             echo $res;
    }

    //修改表单
    public function getEdit($id)
    {

        //查询数据
        $users = DB::table('loveperson')->first();

        // dd($id);
        //查询数据
        $users = DB::table('loveperson')->where('id',$id)->first();

        return view('Admin.loveperson.edit',['users'=>$users]);
    }

    //添加操作

    public function postUpdate(Request $request)

    public function postUpdate(LoveUserRequest $request)
    {
        $data = $request->except('_token');
        // dd($data);

        $res = DB::table('loveperson')->where('id',$request->id)->update($data);

        if($res){
            return redirect('/admin/loveperson/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }


    //审核状态修改
    public function getChange($id)
    {
        //查询状态
        $res = DB::table('loveperson')->where('id',$id)->first();

        if($res->status == 1){

            $data['id']=$id;
            $data['status'] = 0;
            $num = DB::table('loveperson')->where('id',$id)->update($data);
        }else{
            $data['id']=$id;
            $data['status'] =1;
            $num = DB::table('loveperson')->where('id',$id)->update($data);
        }

        if($num){

            return redirect('/admin/loveperson/index')->with('success','状态修改成功');
        }else{

            return back()->with('error','状态修改失败');
        }
    }

}
